<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">楼宇名称</span>

      <el-input v-model="params.name" clearable placeholder="请输入楼宇名称" class="search-main" />

      <el-button type="primary" class="search-btn" @click="doSearch">查询</el-button>

    </div>

    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="hiddendia=true">添加楼宇</el-button>
    </div>

    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="buildingList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="楼宇名称" prop="name" />
        <el-table-column label="层数" prop="floors" />
        <el-table-column label="在管面积（M²）" prop="area" />
        <el-table-column label="物业费(元/M²)" prop="propertyFeePrice" />
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{ scope.row.status === 0 ? '租赁中' : '空置中' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">

          <template #default="scope">
            <!-- #default="scope"定义了一个可访问的当前行数据的模板 -->
            <el-button size="mini" type="text" @click="updateb(scope.row.id)">编辑</el-button>

            <el-button size="mini" type="text" :disabled="scope.row.status===1" @click="deleteb(scope.row.id)">删除</el-button>

          </template>

        </el-table-column>

      </el-table>

    </div>

    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :page-size="params.pageSize"
        :total="total"
        @current-change="pageChange"
      />
    </div>

    <!-- 添加楼宇 -->
    <el-dialog
      :title="title"
      width="580px"
      :visible.sync="hiddendia"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>

          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>

          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>

          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>

        </el-form>
      </div>

      <template #footer>
        <el-button size="mini">取 消</el-button>

        <el-button size="mini" type="primary" @click="doAdd">确 定</el-button>

      </template>

    </el-dialog>

  </div>

</template>

<script>
import { getBuildingAPI, addBuildingAPI, deleteBuildingAPI, updateBuildingAPI } from '@/api/building'
export default {
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        name: null
      },
      total: 0,
      // 弹窗展示
      hiddendia: false,
      // 楼宇列表
      buildingList: [],
      // title标题
      title: '新增楼宇',
      // 新增楼宇列表
      addForm: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      // 校验新增楼宇的列表
      addFormRules: {
        name: [
          { required: true, message: '请输入新增楼宇的名称', tigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入新增层数', tigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入新增在管面积', tigger: 'blur' }
        ],
        propertyFeePrice: [
          {
            required: true, message: '请输入物业费单价', tigger: 'blur'
          }]
      }
    }
  },
  created() {
    this.getBuilding()
  },
  methods: {
    // 楼宇列表渲染
    async getBuilding() {
      const res = await getBuildingAPI(this.params)
      this.buildingList = res.data.rows
      this.total = res.data.total
      // console.log(res)
    },
    // 分页功能
    pageChange(page) {
    // 把点击的页数赋值给请求参数页数
      this.params.page = page
      // 使用最新的请求参数获取列表数据
      this.getBuilding()
    },
    // 搜索功能
    doSearch() {
      //  调用接口前把页数参数重置为1
      this.params.page = 1
      this.getBuilding()
    },
    //  新增楼宇功能 点击确定要做的事情
    async  doAdd() {
      // 进行整体校验
      await this.$refs.addForm.validate((zhi) => {
        if (!zhi) {
          return false
        }
      })
      // 调用新增楼宇的接口
      await addBuildingAPI(this.addForm)
      // 更新页面
      await this.getBuilding()
      // 关闭弹层
      this.hiddendia = false
      // 清楚表单数据
      this.addForm.name = ''
      this.addForm.floors = null
      this.addForm.area = null
      this.addForm.propertyFeePrice = null
    },
    // 楼宇的删除功能
    deleteb(id) {
      // 删除的二次确认
      this.$confirm('确定要删除该楼宇吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 传入要删除的id
        await deleteBuildingAPI(id)
        // 更新页面
        await this.getBuilding()
      }).catch(() => {
        this.$message({
          carBrandtype: 'info',
          message: '已取消删除'
        })
      })
    },
    // 楼宇的编辑功能
    async updateb(id) {
      // 修改标题
      this.title = '修改楼宇'
      // 回填数据
      // 传入要修改的id
      const res = await updateBuildingAPI(id)
      // console.log(res)
      // 回填数据
      // 清楚表单数据
      this.addForm.name = res.data.name
      this.addForm.floors = res.data.floors
      this.addForm.area = res.data.area
      this.addForm.propertyFeePrice = res.data.propertyFeePrice
      // 展示弹层
      this.hiddendia = true
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
